package com.vehicle.controller;

import com.vehicle.biz.SysPermissionBiz;
import com.vehicle.domains.vo.BasePage;
import com.vehicle.domains.vo.Result;
import com.vehicle.domains.vo.req.sysPermission.AddPermissionReq;
import com.vehicle.domains.vo.req.sysPermission.ListPermissionReq;
import com.vehicle.domains.vo.req.sysPermission.UpdatePermissionReq;
import com.vehicle.domains.vo.res.sysPermission.SysPermissionRes;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统权限管理控制器
 * 处理权限的新增、修改、删除、查询及层级关系查询等操作
 */
@RestController
@RequestMapping("/api/permission")
@Slf4j
@Tag(name = "SysPermissionController", description = "系统权限管理相关接口")
public class SysPermissionController {

    @Resource
    private SysPermissionBiz sysPermissionBiz;

    /**
     * 新增权限接口
     * 需传入权限名称、编码、类型、父ID等信息
     */
    @PostMapping("/add")
    @Operation(description = "新增权限接口")
    public Result<Void> addPermission(@RequestBody @Valid AddPermissionReq req) {
        sysPermissionBiz.addPermission(req);
        return Result.success();
    }

    /**
     * 修改权限接口
     * 需传入权限ID，支持修改名称、类型、父ID、状态、排序号等
     */
    @PostMapping("/updatePermission")
    @Operation(description = "修改权限接口")
    public Result<Void> updatePermission(@RequestBody @Valid UpdatePermissionReq req) {
        sysPermissionBiz.updatePermission(req);
        return Result.success();
    }

    /**
     * 删除权限接口
     * 通过权限ID删除（建议逻辑删除，更新status为DELETED）
     */
    @PostMapping("/deletePermission")
    @Operation(description = "删除权限接口")
    public Result<Void> deletePermission(
            @Parameter(schema = @Schema(name = "id", description = "权限id"))
            @RequestParam Long id) {
        sysPermissionBiz.deletePermission(id);
        return Result.success();
    }

    /**
     * 根据ID查询权限接口
     * 返回权限完整信息（含层级关系、状态、排序等）
     */
    @PostMapping("/getPermissionById")
    @Operation(description = "根据id查询权限接口")
    public Result<SysPermissionRes> getPermissionById(
            @Parameter(schema = @Schema(name = "id", description = "权限id"))
            @RequestParam Long id) {
        return Result.success(sysPermissionBiz.getPermissionById(id));
    }

    /**
     * 分页查询权限列表接口
     * 支持按名称、编码、类型、状态、父ID、时间范围筛选
     */
    @PostMapping("/list")
    @Operation(description = "分页查询权限列表接口")
    public Result<BasePage<SysPermissionRes>> list(@RequestBody @Valid ListPermissionReq req) {
        return Result.success(sysPermissionBiz.selectPermissionList(req));
    }
}
